package com.gipson.amc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.entity.Application;
import com.gipson.amc.entity.PageByDataTable;
import com.gipson.amc.service.basicdata.ApplicationService;
import com.gipson.amc.utils.PasswordHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 *  应用管理 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-05-09
 */
@Controller
@RequestMapping("/application")
public class ApplicationController extends BaseController {

    @Resource
    private ApplicationService applicationService;

    @RequestMapping("/list")
    @RequiresPermissions("application.list")
    public String list(){
        return "/basicdata/application/list";
    }

    @RequestMapping("/details")
    @RequiresPermissions("application.details")
    public String details(Model model, String id){
        Application application= applicationService.getById(id==null?"":id);
        String secretKey ="";
        if(application==null){
            application = new Application();
        }else{
            secretKey = new SimpleHash(PasswordHelper.ALGORITHM_NAME, application.getId(), ByteSource.Util.bytes(application.getPriKey()), PasswordHelper.HASH_ITERATIONS).toHex();
        }

        model.addAttribute("application",application);
        model.addAttribute("secretKey",secretKey);
        return "/basicdata/application/details";
    }

    @RequestMapping("/ajaxPageInfo")
    @RequiresPermissions("application.list")
    @ResponseBody
    public DataPageMessage ajaxPageInfo(Application application, PageByDataTable pageByDataTable){
        Page<Application> applicationPage =applicationService.getApplicationPage(application,pageByDataTable);
        return DataPageMessage.ok(applicationPage.getRecords(),(int)applicationPage.getTotal(),(int)applicationPage.getTotal(),pageByDataTable.getSEcho(),"");
    }


    @RequestMapping("/edit")
    @RequiresPermissions(value = {"application.add","application.edit"},logical = Logical.OR)
    public String edit (Model model, String id){
        Application application= applicationService.getById(id==null?"":id);
        if(application==null){
            application = new Application();
        }
        model.addAttribute("application",application);
        return "/basicdata/application/edit";
    }

    @RequestMapping("/del")
    @RequiresPermissions("application.delete")
    @ResponseBody
    public ResponseMessage del (String ids){
        if(StringUtils.isNotBlank(ids)){
            applicationService.delBatch(ids);
        }
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"application.add","application.edit"},logical = Logical.OR)
    @ResponseBody
    public ResponseMessage saveOrUpdate( Application application){
        return  applicationService.saveOrUpdateByApplication(application);
    }
}
